package jp.ircbot.kaitvip.bo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import au.com.bytecode.opencsv.bean.CsvToBean;
import au.com.bytecode.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;

import jp.ircbot.kaitvip.common.cif.SuperBeanIF;

public class FileIO {

	/* Shift_JIS, ISP-2022-JP, EUC-JP Windows-31J */
	/* 出力時に　JISAutoDetect　の指定は不可 */
	private static String outFileCoding = "Shift_JIS";

	/**
	 * 文字列リストをファイルに書き出す
	 *
	 * @param parmmap
	 * @param fileName
	 */
	public static void writeFile(List<String> outputDataList, String fileName) {

		BufferedWriter outFile = null;

		try {
			outFile = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileName), outFileCoding));

			for (String outputData : outputDataList) {
				outFile.write(outputData);
				outFile.newLine();
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// クローズ処理
				outFile.flush();
				outFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * ファイルデータを読み出す
	 *
	 * @param filename
	 */
	public static List<String> readFile(String filename) {

		FileInputStream fi3 = null;
		InputStreamReader ir3 = null;
		BufferedReader br3 = null;
		List<String> inputData = new ArrayList<String>();

		try {

			File chkFile = new File(filename);
			if (!chkFile.exists()) {
				chkFile.createNewFile();
			}

			fi3 = new FileInputStream(filename);
			ir3 = new InputStreamReader(fi3, outFileCoding);
			br3 = new BufferedReader(ir3);

			// 読み込み
			System.out.println("一行ずつ読む");
			String line;
			while ((line = br3.readLine()) != null) {
				// 改行がカットされてるので、printlnになる。
				System.out.println(line);
				inputData.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// クローズ処理
				fi3.close();
				ir3.close();
				br3.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return inputData;
	}

	/**
	 * Beanオブジェクトの情報をファイルに保存する<br>
	 * outputBeanListの長さが0の場合は処理しない。
	 *
	 * @param outputBeanList
	 * @param fileName
	 * @throws FileNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public static void writeCSVFile(List<SuperBeanIF> outputBeanList, String fileName) throws FileNotFoundException {
		if (outputBeanList.size() == 0) {
			return;
		}

		HeaderColumnNameTranslateMappingStrategy<SuperBeanIF> strat = new HeaderColumnNameTranslateMappingStrategy<SuperBeanIF>();
		strat.setType((Class<SuperBeanIF>) outputBeanList.get(0).getClass());

		Map<String, String> map = outputBeanList.get(0).getMapping();

		strat.setColumnMapping(map);
		CsvToBean<SuperBeanIF> csv = new CsvToBean<SuperBeanIF>();
		List<SuperBeanIF> list = csv.parse(strat, new FileReader(fileName));

	}

	/**
	 * Beanオブジェクトの情報をCSVファイルから読み出す
	 *
	 * @param beanFormat
	 * @param fileName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<SuperBeanIF> readCSVFile(final SuperBeanIF beanFormat, String fileName) throws FileNotFoundException {
		HeaderColumnNameTranslateMappingStrategy<SuperBeanIF> strat = new HeaderColumnNameTranslateMappingStrategy<SuperBeanIF>();
		strat.setType((Class<SuperBeanIF>) beanFormat.getClass());

		Map<String, String> map = beanFormat.getMapping();

		strat.setColumnMapping(map);
		CsvToBean<SuperBeanIF> csv = new CsvToBean<SuperBeanIF>();
		List<SuperBeanIF> list = csv.parse(strat, new FileReader(fileName));
		for (SuperBeanIF bean : list) {
			System.out.println(bean);
		}
		return list;
	}

}
